<?php

require_once '../bean/Disciplina.php';
class DisciplinaDataManager
{
    private static $instance;

    public static function getInstance()
    {
        if(self::$instance == null)
        {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    public function insertDisciplina($connection, $nome, $descrizione)
    {
        $query = "Insert into Discipline (nome,descrizione) VALUES(?, ?) ";
        $stmt = $connection->prepare($query);
        $stmt->bind_param( "ss", $nome, $descrizione);
        $stmt->execute();
    }

    public function  getDiscipline($connection)
    {
        $query = "Select codice, nome from Discipline where nome !='init' ";
        $result = $connection->query($query);
        return $result;
    }

    public function getDisciplineUtente($db)
    {
        $query = sprintf("Select codiceDisciplina from UtenteDisciplina, Discipline where Discipline.codice=UtenteDisciplina.codiceDisciplina AND Discipline.nome !='init' AND UtenteDisciplina.codiceUtente = %d",$_COOKIE["codiceUtente"]);
        if($stmt = $db->prepare($query))
            $stmt->execute();

        $stmt->bind_result($codiceDisciplina);
        $lista = array();

        while ($stmt->fetch())
        {
            $lista[] = $codiceDisciplina;
        }

        $stmt->close();

        return $lista;
    }

    public function getDisciplineByUtente($db, $utente)
    {
        $query = sprintf("Select codiceDisciplina from UtenteDisciplina, Discipline where Discipline.codice=UtenteDisciplina.codiceDisciplina AND Discipline.nome !='init' AND UtenteDisciplina.codiceUtente = %d",$utente);
        if($stmt = $db->prepare($query))
            $stmt->execute();

        $stmt->bind_result($codiceDisciplina);
        $lista = array();
        
        while ($stmt->fetch())
        {
            $lista[] = $codiceDisciplina;
        }

        $stmt->close();

        return $lista;
    }


    public function getDisciplina($connection, $codiceDisciplina)
    {
        $query = sprintf("Select codice, nome, descrizione from Discipline where codice = %d",$codiceDisciplina);
        if($stmt = $connection->prepare($query))
            $stmt->execute();

        $stmt->bind_result($codiceDisc, $name, $descrizione);


        if ($stmt->fetch())
        {
            $disc = new Disciplina();
            $disc->setCodice($codiceDisc);
            $disc->setDescrizione(stripslashes($descrizione));
            $disc->setNome(stripslashes($name));

        }

        $stmt->close();

        return $disc;
    }
}
?>
